const express = require('express');
// 创建一个路由
const router = express.Router();
const mysql = require('../../conn');
const getDate = require('../../utils');


// 查询评论
router.get('/get', (req, res) => {
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的查询语句
    let sql = `select * from message`;
    let cookbook_ids = [];
    let user_ids = [];
    let title_result = [];
    let name_result = [];
    mysql.query(sql, (err, results) => {
        if (err) return console.log(err);
        results.forEach(item => {
            cookbook_ids.push(item.cook_book_id);
            user_ids.push(item.user_id);
        });
        for (let i = 0; i < cookbook_ids.length; i++) {
            mysql.query(`select cook_book_title from cookbook where cook_book_id = ?`, [cookbook_ids[i]], (err, result) => {
                if (err) return console.log(err);
                obj.code = 200;
                obj.desc = '成功';
                title_result.push(result[0].cook_book_title);

            });
        }
        for (let i = 0; i < user_ids.length; i++) {
            mysql.query(`select user_name from user where user_id = ?`, [user_ids[i]], (err, result) => {
                if (err) return console.log(err);
                obj.code = 200;
                obj.desc = '成功';
                name_result.push(result[0].user_name);

            });
        }
        setTimeout(() => {
            results.forEach((res, index) => {
                res.cook_book_title = title_result[index];
                res.user_name = name_result[index];
            })
            obj.data = results;
            res.send(obj);
        }, 1000);


    });
});

// 新增评论
router.post('/add', (req, res) => {
    let { cook_book_id, user_id, content, like_num } = req.body;
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    let time = getDate();

    // 数据库的增加语句
    let sql = `insert into message (cook_book_id,user_id,content,time,like_num) value (?,?,?,?,?)`;

    mysql.query(sql, [cook_book_id, user_id, content, time, like_num], (err, results) => {
        if (err) return console.log(err);
        obj.code = 0;
        obj.desc = '成功';
        obj.data = results;
        res.send(obj);

    });
});

// 根据cook_book_id查询评论
router.get('/info', (req, res) => {
    let { cook_book_id } = req.query;
    let messages = []
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的查询语句
    let sql = `select * from message where cook_book_id = ?`;
    mysql.query(sql, [cook_book_id], (err, results) => {
        if (err) return console.log(err);

        messages = results;
        // console.log(messages);
        for (let i = 0; i < messages.length; i++) {
            mysql.query(`select user_name from user where user_id = ?`, [messages[i].user_id], (err, result) => {
                messages[i].user_name = result[0].user_name;
                // messages[i].user_name = result[i].user_name
            })

        }
        setTimeout(() => {
            obj.code = 200;
            obj.desc = '成功';
            obj.data = messages;
            res.send(obj);

        }, 1000);

    });
});

// 删除

router.put('/delete', (req, res) => {
    let { message_id } = req.body;
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的增加语句
    let sql = `delete from message where message_id in (?)`;

    mysql.query(sql, [message_id], (err, results) => {
        if (err) return console.log(err);
        obj.code = 0;
        obj.desc = '成功';
        // obj.data = results;
        res.send(obj);
    });
});

// 修改
router.put('/update', (req, res) => {
    let { user_id, content, like_num, message_id } = req.body;
    let time = getDate();

    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的增加语句
    let sql = `update message set content=?,like_num=? , time=? where message_id in (?);`;

    mysql.query(sql, [content, like_num, time, message_id], (err, results) => {
        if (err) return console.log(err);
        obj.code = 0;
        obj.desc = '成功';
        obj.data = results;
        res.send(obj);
    });
});

// 点赞接口
router.post('/like', (req, res) => {
    let { message_id } = req.body;
    // let create_time = getDate();
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    let like_num;
    // 数据库的增加语句
    mysql.query(`select like_num from message where message_id = ?`, [message_id], (err, result) => {
        if (err) return console.log(err);
        like_num = JSON.parse(JSON.stringify(result))[0].like_num;
        like_num = like_num + 1;
        let sql = `update message set like_num=? where message_id in (?);`;

        mysql.query(sql, [like_num, message_id], (err, results) => {
            if (err) return console.log(err);
            obj.code = 0;
            obj.desc = '成功';
            obj.data = results;
            res.send(obj);
        });
    })


});

// 取消点赞接口
router.post('/unlike', (req, res) => {
    let { message_id } = req.body;
    // let create_time = getDate();
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    let like_num;
    // 数据库的增加语句
    mysql.query(`select like_num from message where message_id = ?`, [message_id], (err, result) => {
        if (err) return console.log(err);
        like_num = JSON.parse(JSON.stringify(result))[0].like_num;
        like_num = like_num - 1;
        let sql = `update message set like_num=? where message_id in (?);`;

        mysql.query(sql, [like_num, message_id], (err, results) => {
            if (err) return console.log(err);
            obj.code = 0;
            obj.desc = '成功';
            obj.data = results;
            res.send(obj);
        });
    })


});


module.exports = router;